﻿package mortal.game.view.copy.sixBoss
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.mui.controls.*;
    import extend.language.*;
    import mortal.common.cd.*;
    import mortal.component.gconst.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.rules.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.alertwins.*;
    import mortal.game.view.copy.*;
    import mortal.game.view.copy.sixBoss.view.*;
    import mortal.mvc.core.*;

    public class SixBossController extends CopyControllerBase
    {
        private var _isSixBossCopyOpen:Boolean;
        private var _currSelBoss:int;
        private var _secTimerViewData:SecTimerViewData;
        private var _secTimerViewData2:SecTimerViewData;
        private var _isInActivity:Boolean;

        public function SixBossController()
        {
            return;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.SixBossEnterCopy, this.onEnterSixBossHandler);
            Dispatcher.addEventListener(EventName.ShopMallOpenAndSelect, this.openSixBossEnterWin);
            Dispatcher.addEventListener(EventName.SelectEntityChange, this.onChangeBossHandler);
            Dispatcher.addEventListener(EventName.SixBossOpenEnterWin, this.onOpenEnterWinHandler);
            NetDispatcher.addCmdListener(ServerCommand.RoleLevelUpdate, this.onLevelUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.SixBossCopyOpen, this.onSixBossCopyOpenHandler);
            NetDispatcher.addCmdListener(ServerCommand.SixBossCopyClose, this.onSixBossCopyCloseHandler);
            NetDispatcher.addCmdListener(ServerCommand.SixBossCopyEnd, this.onSixBossCopyEndHandler);
            NetDispatcher.addCmdListener(ServerCommand.SixBossShowResultInfo, this.onShowResultHandler);
            return;
        }// end function

        override public function enterCopy(param1:TCopy) : void
        {
            super.enterCopy(param1);
            AIManager.cancelAll();
            SixBossEnterWin.instance.hide();
            SixBossDamageInfoWin.instance.show();
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskShowHideTrackExceptBtn, false));
            return;
        }// end function

        override public function leaveCopy() : void
        {
            super.leaveCopy();
            SixBossDamageInfoWin.instance.hide();
            if (SixBossInfoWin.hasInstance() && !SixBossInfoWin.instance.isHide)
            {
                SixBossInfoWin.instance.hide();
            }
            this._currSelBoss = 0;
            return;
        }// end function

        private function onEnterSixBossHandler(event:DataEvent) : void
        {
            CopyUtil.copyCheck(this.enterSixBossCopy);
            return;
        }// end function

        private function enterSixBossCopy() : void
        {
            GameProxy.copy.enterMonsterSiege();
            return;
        }// end function

        private function onSixBossCopyOpenHandler(param1:Object) : void
        {
            this._isSixBossCopyOpen = true;
            this.onSixBossCopyOpen(true);
            return;
        }// end function

        private function onSixBossCopyOpen(param1:Boolean = false) : void
        {
            var _loc_4:Date = null;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_2:* = GameConst.sixBossOpenLevel;
            if (cache.role.entityInfo.level < _loc_2)
            {
                return;
            }
            var _loc_3:* = cache.copy.sixBoss.sixBossTimeInfo;
            if (_loc_3)
            {
                _loc_4 = ClockManager.instance.nowDate;
                _loc_5 = (_loc_3.startDt.time - _loc_4.time) * 0.001;
                _loc_6 = (_loc_3.endDt.time - _loc_4.time) * 0.001;
                SixBossIcon.instance.show();
                if (_loc_5 > 0)
                {
                    this._isInActivity = false;
                    SixBossIcon.instance.setLeftTimeAndState(_loc_5, 1);
                    if (!this._secTimerViewData2)
                    {
                        this._secTimerViewData2 = new SecTimerViewData();
                        this._secTimerViewData2.setLeftTime(_loc_5);
                        this._secTimerViewData2.timeOutCaller = this.showEndLeftTime;
                    }
                }
                else
                {
                    this._isInActivity = true;
                    SixBossIcon.instance.playEffect();
                    SixBossIcon.instance.setLeftTimeAndState(_loc_6, 2);
                    if (param1 && !GameMapUtil.curMapState.isSixBossMap)
                    {
                        SixBossEnterWin.instance.show();
                    }
                }
            }
            return;
        }// end function

        private function showEndLeftTime() : void
        {
            var _loc_2:Date = null;
            var _loc_3:int = 0;
            if (this._secTimerViewData2)
            {
                this._secTimerViewData2.dispose();
                this._secTimerViewData2 = null;
            }
            SixBossIcon.instance.playEffect();
            var _loc_1:* = cache.copy.sixBoss.sixBossTimeInfo;
            if (_loc_1)
            {
                _loc_2 = ClockManager.instance.nowDate;
                _loc_3 = (_loc_1.endDt.time - _loc_2.time) * 0.001;
                SixBossIcon.instance.setLeftTimeAndState(_loc_3, 2);
            }
            if (!GameMapUtil.curMapState.isSixBossMap)
            {
                SixBossEnterWin.instance.show();
            }
            this._isInActivity = true;
            return;
        }// end function

        private function onSixBossCopyCloseHandler(param1:Object) : void
        {
            this._isSixBossCopyOpen = false;
            this._isInActivity = false;
            if (SixBossEnterWin.hasInstance() && !SixBossEnterWin.instance.isHide)
            {
                SixBossEnterWin.instance.hide();
            }
            SixBossIcon.instance.hide();
            return;
        }// end function

        private function onSixBossCopyEndHandler(param1:Object) : void
        {
            if (!this._secTimerViewData)
            {
                this._secTimerViewData = new SecTimerViewData();
                this._secTimerViewData.setLeftTime(10);
                this._secTimerViewData.timeOutCaller = this.showAlertWin;
            }
            return;
        }// end function

        private function showAlertWin() : void
        {
            var onCloseHandler:Function;
            onCloseHandler = function (param1:int) : void
            {
                GameProxy.copy.leaveCopy();
                return;
            }// end function
            ;
            this._secTimerViewData.dispose();
            this._secTimerViewData = null;
            if (!GameMapUtil.curMapState.isSixBossMap)
            {
                return;
            }
            Alert.mode = Alert.Mode_NoneNotModal;
            Alert.alertWinRenderer = TimerAlertWin;
            Alert.buttonWidth = 70;
            Alert.timerOut = 10;
            Alert.okLabel = Language.getString(40939);
            Alert.show(Language.getString(40940), null, Alert.OK, null, onCloseHandler);
            return;
        }// end function

        private function onLevelUpdateHandler(param1:Object) : void
        {
            if (this._isSixBossCopyOpen)
            {
                this.onSixBossCopyOpen();
            }
            return;
        }// end function

        private function onChangeBossHandler(event:DataEvent) : void
        {
            var _loc_3:int = 0;
            if (!GameMapUtil.curMapState.isSixBossMap)
            {
                return;
            }
            var _loc_2:* = ThingUtil.selectEntity;
            if (_loc_2 && _loc_2 is MonsterPlayer && !BossRule.isCollectBoss(_loc_2 as MonsterPlayer))
            {
                _loc_3 = (_loc_2 as MonsterPlayer).bossInfo.code;
                if (this._currSelBoss != _loc_3)
                {
                    GameProxy.copy.changeBoss((_loc_2 as MonsterPlayer).entityInfo.entityInfo.entityId);
                    this._currSelBoss = _loc_3;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.SixBossChangeBoss));
                }
            }
            return;
        }// end function

        private function onOpenEnterWinHandler(event:DataEvent) : void
        {
            if (SixBossEnterWin.instance.isHide && !GameMapUtil.curMapState.isSixBossMap)
            {
                SixBossEnterWin.instance.show();
            }
            return;
        }// end function

        private function openSixBossEnterWin(event:DataEvent) : void
        {
            var _loc_2:* = event.data as String;
            if (_loc_2 == "show me the sixboss")
            {
                SixBossEnterWin.instance.show();
            }
            return;
        }// end function

        private function onShowResultHandler(param1:Object) : void
        {
            var _loc_2:* = param1 as SMonsterSiegeResultMsg;
            if (_loc_2)
            {
                SixBossKillRewardWin.instance.show();
                SixBossKillRewardWin.instance.data = _loc_2;
            }
            return;
        }// end function

        public function set currSelBoss(param1:int) : void
        {
            this._currSelBoss = param1;
            return;
        }// end function

        public function get currSelBoss() : int
        {
            return this._currSelBoss;
        }// end function

        public function get isInActivity() : Boolean
        {
            return this._isInActivity;
        }// end function

    }
}
